package com.bestcem.xm.txtanls.dao.impl.mysql;

import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.dao.SurveyTxtanlsFieldDao;
import com.bestcem.xm.txtanls.dao.mapper.SurveyTxtanlsFieldMapper;
import com.bestcem.xm.txtanls.entity.mysql.SurveyTxtanlsFieldEntity;
import com.bestcem.xm.txtanls.entity.pojo.SurveyTxtanlsFieldDO;
import com.bestcem.xm.txtanls.enums.SurveyFieldTypeEnum;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文本分析问卷分析字段表 实现类
 *
 * @author jie.yao <jie.yao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/27 15:28
 */
@Repository
public class SurveyTxtanlsFieldDaoImpl implements SurveyTxtanlsFieldDao {

    @Resource
    private SurveyTxtanlsFieldMapper surveyTxtanlsFieldMapper;

    @Override
    public List<SurveyTxtanlsFieldDO> findProjectAllTextFiled(String projectId) {
        List<SurveyTxtanlsFieldEntity> surveyTxtanlsFieldEntityList = surveyTxtanlsFieldMapper.listById(projectId);
        return listToDO(surveyTxtanlsFieldEntityList);
    }

    @Override
    public String findFieldColName(String id) {
        return surveyTxtanlsFieldMapper.getFieldColNameById(id);
    }

    @Override
    public SurveyTxtanlsFieldDO insertTimeField(String projectId, String surveyId) {
        SurveyTxtanlsFieldEntity entity = new SurveyTxtanlsFieldEntity();
        entity.setSurveyFieldId(new ObjectId().toHexString());
        entity.setProjectId(projectId);
        entity.setSurveyId(surveyId);
        entity.setFieldName("time");
        entity.setType(SurveyFieldTypeEnum.TIME.getIndex());
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        surveyTxtanlsFieldMapper.insert(entity);
        SurveyTxtanlsFieldEntity surveyTxtanlsFieldEntity = surveyTxtanlsFieldMapper.getById(entity.getSurveyFieldId());
        return toDO(surveyTxtanlsFieldEntity);
    }

    @Override
    public SurveyTxtanlsFieldDO insertTextField(String projectId, String surveyId, String fieldId, String fieldTitle) {
        SurveyTxtanlsFieldEntity entity = new SurveyTxtanlsFieldEntity();
        entity.setSurveyFieldId(new ObjectId().toHexString());
        entity.setProjectId(projectId);
        entity.setSurveyId(surveyId);
        entity.setFieldName(fieldId);
        entity.setColName(fieldTitle);
        entity.setType(SurveyFieldTypeEnum.TEXT.getIndex());
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        surveyTxtanlsFieldMapper.insert(entity);
        SurveyTxtanlsFieldEntity surveyTxtanlsFieldEntity = surveyTxtanlsFieldMapper.getById(entity.getSurveyFieldId());
        return toDO(surveyTxtanlsFieldEntity);
    }

    @Override
    public boolean deleteById(String id) {
        return surveyTxtanlsFieldMapper.deleteById(id) > Constants.INT_ZERO;
    }

    @Override
    public boolean deleteProjectAllFiled(String projectId) {
        return surveyTxtanlsFieldMapper.deleteByProjectId(projectId) > Constants.INT_ZERO;
    }

    /**
     * List<entity> to List<DO>
     *
     * @param surveyTxtanlsFieldEntityList entityList
     * @return DOList
     */
    private List<SurveyTxtanlsFieldDO> listToDO(List<SurveyTxtanlsFieldEntity> surveyTxtanlsFieldEntityList) {
        return surveyTxtanlsFieldEntityList.stream().map(this::toDO).collect(Collectors.toList());
    }

    /**
     * entity to DO
     *
     * @param surveyTxtanlsFieldEntity entity
     * @return DO
     */
    private SurveyTxtanlsFieldDO toDO(SurveyTxtanlsFieldEntity surveyTxtanlsFieldEntity) {
        SurveyTxtanlsFieldDO surveyTxtanlsFieldDO = new SurveyTxtanlsFieldDO();
        surveyTxtanlsFieldDO.setId(surveyTxtanlsFieldEntity.getSurveyFieldId());
        surveyTxtanlsFieldDO.setProjectId(surveyTxtanlsFieldEntity.getProjectId());
        surveyTxtanlsFieldDO.setSurveyId(surveyTxtanlsFieldEntity.getSurveyId());
        surveyTxtanlsFieldDO.setSurveyFieldName(surveyTxtanlsFieldEntity.getFieldName());
        surveyTxtanlsFieldDO.setSurveyColName(surveyTxtanlsFieldEntity.getColName());
        surveyTxtanlsFieldDO.setType(surveyTxtanlsFieldEntity.getType());
        surveyTxtanlsFieldDO.setCreatedId(surveyTxtanlsFieldEntity.getCreatorId());
        surveyTxtanlsFieldDO.setCreatedDate(surveyTxtanlsFieldEntity.getCreateTime());
        surveyTxtanlsFieldDO.setUpdatedId(surveyTxtanlsFieldEntity.getOperatorId());
        surveyTxtanlsFieldDO.setUpdatedDate(surveyTxtanlsFieldEntity.getUpdateTime());
        return surveyTxtanlsFieldDO;
    }
}
